//
//  VoiceRecordButton.m
//  YunShouHu
//
//  Created by WangQiang on 16/6/24.
//  Copyright © 2016年 WangQiang. All rights reserved.
//

#import "MsgVoiceRecordButton.h"
#import "VoiceRecordLoadingView.h"
#import "WQVoiceTool.h"



#import <AVFoundation/AVFoundation.h>
@interface MsgVoiceRecordButton()
@property (weak ,nonatomic) VoiceRecordLoadingView *loadingView;
@property (copy ,nonatomic) NSString *recordName;
//@property (assign ,nonatomic) BOOL hasRecord;
@end
@implementation MsgVoiceRecordButton
-(void)recordCommonInit{
    [self addTarget:self action:@selector(startRecord) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(stopRecord) forControlEvents:UIControlEventTouchUpInside];
//    [self addTarget:self action:@selector(cancelRecord) forControlEvents:UIControlEventTouchCancel];
    [self addTarget:self action:@selector(cancelRecord) forControlEvents:UIControlEventTouchUpOutside];
    self.backgroundColor = [UIColor lightGrayColor] ;
    [self addCornerRadius:5.0];
    self.layer.borderWidth = 0.5;
//    self.layer.borderColor = WHITE_BG.CGColor;
    [self setTitle:NSLocalizedString(@"按住说话", nil) forState:UIControlStateNormal];
    [self setTitle:NSLocalizedString(@"正在录音...", nil) forState:UIControlStateHighlighted];
}

-(instancetype)initVocieRecordButton{
    if(self = [super init]){
        [self recordCommonInit];
    }
    return self;
}
-(void)dealloc{
   
}
-(void)setHighlighted:(BOOL)highlighted{
    if(!highlighted){
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [super setHighlighted:highlighted];
            self.backgroundColor = [UIColor lightGrayColor] ;
        });
    }else{
     
        [super setHighlighted:highlighted];
        self.backgroundColor = [UIColor darkGrayColor];
    }
   
}
//-(void)stopRecord{
//    typeof(self) WeakSelf = self;
//    [[MsgVoiceTool sharedMsgVoiceTool] stopRecord:^(CGFloat length, NSData *voiceData, NSError *error) {
//        if(error){
//            [WeakSelf.loadingView removeFromSuperview];
//            [MBProgressHUD showTipError:error];
//        }else{
//            [WeakSelf.loadingView hideWithCompeletion:^(BOOL finsh) {
//                if([WeakSelf.delegate respondsToSelector:@selector(voiceRecordFinshRecord:recordData:duration:)]){
//                    [WeakSelf.delegate voiceRecordFinshRecord:WeakSelf.recordName recordData:voiceData duration:length];
//                }
//            }];
//        }
//    }];
//}
//-(void)startRecord{
//    NSString *name = [NSString stringWithFormat:@"%@.wav",[NSString dateTime]];
//    self.recordName = [[MsgVoiceTool sharedMsgVoiceTool] cachePathForVoiceName:name];
//    NSError *error = [[MsgVoiceTool sharedMsgVoiceTool] startRecordWithPath:self.recordName];
//    if(error){
//        [MBProgressHUD showTipError:error];
//        return;
//    }
//    VoiceRecordLoadingView *loadingView = [[VoiceRecordLoadingView alloc] initWithFrame:CGRectMake(0, 0, APP_WIGHT, APP_HEIGHT - 100)];
//    self.loadingView = loadingView;
//    [loadingView show];
//   
//}
//-(void)cancelRecord{
//    [[MsgVoiceTool sharedMsgVoiceTool] cancelRecord];
//    if(self.loadingView){
//        [self.loadingView hideWithCompeletion:^(BOOL finsh) {
//            [MBProgressHUD showTipMessage:NSLocalizedString(@"取消了录音", nil)];
//        }];
//    }
//}

@end
